import {
  postUserListData,
  deleteUserById,
  createUser,
  updateUser,
  postPageListData,
  deletePageById,
  createPage,
  updatePage
} from '@/service/main/system/system'
import { defineStore } from 'pinia'

interface ISystemState {
  userList: any[]
  userTotalCount: number
  pageList: any[]
  pageTotalCount: number
}

const useSystemStore = defineStore('system', {
  state(): ISystemState {
    return {
      userList: [],
      userTotalCount: 0,
      pageList: [],
      pageTotalCount: 0
    }
  },
  actions: {
    async postUserListAction(queryInfo: any) {
      const userListResult = await postUserListData(queryInfo)
      const { totalCount, list } = userListResult.data
      this.userList = list
      this.userTotalCount = totalCount
    },
    // 删除数据
    async deleteUserByIdAction(id: number) {
      const deleteResult = await deleteUserById(id)
      console.log(deleteResult)
    },
    // 新增数据
    async createUserAction(userInfo: any) {
      const createResult = await createUser(userInfo)
      console.log(createResult)
    },
    // 编辑数据
    async updateUserAction(id: number, userInfo: any) {
      const updateResult = await updateUser(id, userInfo)
      console.log(updateResult)
    },
    async postPageListAction(pageName: string, queryInfo: any) {
      const pageListResult = await postPageListData(pageName, queryInfo)
      const { totalCount, list } = pageListResult.data
      this.pageList = list
      this.pageTotalCount = totalCount
    },
    // 删除数据
    async deletePageByIdAction(pageName: string, id: number) {
      const deleteResult = await deletePageById(pageName, id)
      console.log(deleteResult)
    },
    // 新增数据
    async createPageAction(pageName: string, pageInfo: any) {
      const createResult = await createPage(pageName, pageInfo)
      console.log(createResult)
    },
    // 编辑数据
    async updatePageAction(pageName: string, id: number, userInfo: any) {
      const updateResult = await updatePage(pageName, id, userInfo)
      console.log(updateResult)
    }
  }
})

export default useSystemStore
